package com.zret.oasys.operation.attendance;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zret.oasys.dbtools.PageData;
import com.zret.oasys.entity.Attence;
import com.zret.oasys.entity.AttenceTable;
import com.zret.oasys.entity.Dept;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.entity.Position;
import com.zret.oasys.json.entity.GetTableDataJson;
import com.zret.oasys.service.DeptService;
import com.zret.oasys.service.EmpService;
import com.zret.oasys.service.PositionService;
import com.zret.oasys.service.impl.DeptServiceImpl;
import com.zret.oasys.service.impl.EmpServiceImpl;
import com.zret.oasys.service.impl.PositionServiceImpl;

public class GetAttendanceByDate implements AttendanceServletOperation {
	

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//设置json编码
		response.setContentType("application/json;charset=utf-8");
		//创建业务层对象
		DeptService deptService=new DeptServiceImpl();
		PositionService positionService=new PositionServiceImpl();
		EmpService empService=new EmpServiceImpl();
		
		//获取输入日期的关键字
		String datekeyword ="";
		if (request.getParameter("key")!=null) {
			datekeyword = request.getParameter("key");
		}
		System.out.println("datekeyword:"+datekeyword);
		//获取page参数
		int page = Integer.parseInt(request.getParameter("page"));
		//获取pageSize参数
		int pagesize = Integer.parseInt(request.getParameter("limit"));
		//查询考勤表分页得到PageData<Attence>
		PageData<AttenceTable> attencePD = attendanceService.getAttendanceByDatePage(page, pagesize, datekeyword);
		//PageData<AttenceTable> attencePD = attendanceService.getAttendanceByDatePage(1, 40, "");
		//获取attencePD的data
		List<AttenceTable> attenceTableList = attencePD.getData();
		//创建通用json的实体类对象
		GetTableDataJson<AttenceTable> getattenceTableJson=new GetTableDataJson<AttenceTable>(0, "", attencePD.getTotalCount(), attenceTableList);
		//创建Gson对象，加入时间格式
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		//把通用json的实体类对象转换为json字符串
		String  getattenceTableJsonStr = gson.toJson(getattenceTableJson);
		System.out.println(getattenceTableJsonStr);
		//创建打印页面的输出流
		PrintWriter out = response.getWriter();
		
		//响应json字符串到页面
		out.print(getattenceTableJsonStr);
		//关闭流
		out.close();
		
		
		

	}

}
